package com.shuiyun.jdbc.sharding;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author shuiyun
 */
public class OrderPreciseShardingData implements PreciseShardingAlgorithm<Long> {

    private final static String DATA_SOURCE_NAME = "m";


    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {
        Long value = shardingValue.getValue();
        value = value % 2 + 1;
        AtomicReference<String> result = new AtomicReference<>();
        Long finalValue = value;
        availableTargetNames.forEach(dataName -> {
            if (dataName.equals(DATA_SOURCE_NAME + finalValue)){
                result.set(dataName);
            }
        });
        return result.get();
    }
}
